/*
 * jxSwing Project (2014)
 * by Denilson Edinaldo Pinto
 * LGPL
 * Copyright (C) 2014
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.trayIcon;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;

import org.jxSwing.i18n.I18N;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.trayIcon
 * @date 21/01/2014 - 19:40:30
 */
public class TrayIconUtil {
	
	private SystemTray systemTray = null;
	
	/**
	 * @param icon
	 * @throws Exception
	 * @throws AWTException
	 */
	public TrayIconUtil(final Image icon) throws Exception, AWTException {
	
		this(icon, null);
	}
	
	/**
	 * @param icon
	 * @param tooltip
	 * @throws Exception
	 * @throws AWTException
	 */
	public TrayIconUtil(final Image icon, final String tooltip) throws Exception, AWTException {
	
		this(null, icon, tooltip);
	}
	
	/**
	 * @param icon
	 * @throws Exception
	 * @throws AWTException
	 */
	public TrayIconUtil(final ImageIcon icon) throws Exception, AWTException {
	
		this(icon.getImage(), null);
	}
	
	/**
	 * @param icon
	 * @param tooltip
	 * @throws Exception
	 * @throws AWTException
	 */
	public TrayIconUtil(final ImageIcon icon, final String tooltip) throws Exception, AWTException {
	
		this(null, icon.getImage(), tooltip);
	}
	
	/**
	 * @param popupMenu
	 * @param icon
	 * @param tooltip
	 * @throws Exception
	 * @throws AWTException
	 */
	public TrayIconUtil(final PopupMenu popupMenu, final Image icon, final String tooltip) throws Exception, AWTException {
	
		this(popupMenu, icon, tooltip, null);
	}
	
	/**
	 * @param popupMenu
	 * @param icon
	 * @param tooltip
	 * @param mouseListener
	 * @throws Exception
	 * @throws AWTException
	 */
	public TrayIconUtil(final PopupMenu popupMenu, final Image icon, final String tooltip, final MouseListener mouseListener) throws Exception, AWTException {
	
		this(popupMenu, icon, tooltip, mouseListener, null);
	}
	
	/**
	 * @param popupMenu
	 * @param icon
	 * @param tooltip
	 * @param mouseListener
	 * @param mouseMotionListener
	 * @throws Exception
	 * @throws AWTException
	 */
	public TrayIconUtil(final PopupMenu popupMenu, final Image icon, final String tooltip, final MouseListener mouseListener, final MouseMotionListener mouseMotionListener) throws Exception, AWTException {
	
		//
		if(SystemTray.isSupported()) {
			
			systemTray = SystemTray.getSystemTray();
			
			getSystemTray().add(new TrayIcon(icon, tooltip, popupMenu));
			
			getTrayIcon().setImageAutoSize(true);
			
			getTrayIcon().addMouseListener(mouseListener);
			
			getTrayIcon().addMouseMotionListener(mouseMotionListener);
			
		}else {
			
			throw new Exception(I18N.system().notSupportedSystemTrayIcon());
		}
	}
	
	/**
	 * @param popupMenu
	 * @param icon
	 * @param tooltip
	 * @throws Exception
	 * @throws AWTException
	 */
	public TrayIconUtil(final PopupMenu popupMenu, final ImageIcon icon, final String tooltip) throws Exception, AWTException {
	
		this(popupMenu, icon.getImage(), tooltip, null);
	}
	
	/**
	 * @param popupMenu
	 * @param icon
	 * @param tooltip
	 * @param mouseListener
	 * @throws Exception
	 * @throws AWTException
	 */
	public TrayIconUtil(final PopupMenu popupMenu, final ImageIcon icon, final String tooltip, final MouseListener mouseListener) throws Exception, AWTException {
	
		this(popupMenu, icon.getImage(), tooltip, mouseListener, null);
	}
	
	/**
	 * @param popupMenu
	 * @param icon
	 * @param tooltip
	 * @param mouseListener
	 * @param mouseMotionListener
	 * @throws Exception
	 * @throws AWTException
	 */
	public TrayIconUtil(final PopupMenu popupMenu, final ImageIcon icon, final String tooltip, final MouseListener mouseListener, final MouseMotionListener mouseMotionListener) throws Exception, AWTException {
	
		//
		if(SystemTray.isSupported()) {
			
			systemTray = SystemTray.getSystemTray();
			
			getSystemTray().add(new TrayIcon(icon.getImage(), tooltip, popupMenu));
			
			getTrayIcon().setImageAutoSize(true);
			
			getTrayIcon().addMouseListener(mouseListener);
			
			getTrayIcon().addMouseMotionListener(mouseMotionListener);
			
		}else {
			
			throw new Exception(I18N.system().notSupportedSystemTrayIcon());
		}
	}
	
	/**
	 * @param actionListener
	 *        the actionListener to set
	 */
	public final void addActionListener(final ActionListener actionListener) {
	
		//
		if(getTrayIcon() != null) {
			
			getTrayIcon().addActionListener(actionListener);
		}
	}
	
	/**
	 * @param mouseListener
	 *        the mouseListener to set
	 */
	public final void addMouseListener(final MouseListener mouseListener) {
	
		//
		if(getTrayIcon() != null) {
			
			getTrayIcon().addMouseListener(mouseListener);
		}
	}
	
	/**
	 * @param mouseMotionListener
	 *        the mouseMotionListener to set
	 */
	public final void addMouseMotionListener(final MouseMotionListener mouseMotionListener) {
	
		//
		if(getTrayIcon() != null) {
			
			getTrayIcon().addMouseMotionListener(mouseMotionListener);
		}
	}
	
	/**
	 * @return the icon or null
	 */
	public final Image getIcon() {
	
		return getTrayIcon() != null ? getTrayIcon().getImage() : null;
	}
	
	/**
	 * @return the mouseListeners. Never return null.
	 */
	public final MouseListener[] getMouseListener() {
	
		return getTrayIcon() != null ? getTrayIcon().getMouseListeners() : new MouseListener[0];
	}
	
	/**
	 * @return the mouseMotionListener. Never return null.
	 */
	public final MouseMotionListener[] getMouseMotionListener() {
	
		return getTrayIcon() != null ? getTrayIcon().getMouseMotionListeners() : new MouseMotionListener[0];
	}
	
	/**
	 * @return the popupMenu or null
	 */
	public final PopupMenu getPopupMenu() {
	
		return getTrayIcon() != null ? getTrayIcon().getPopupMenu() : null;
	}
	
	/**
	 * @return the systemTray or null
	 */
	public final SystemTray getSystemTray() {
	
		return systemTray;
	}
	
	/**
	 * @return the tooltip or null
	 */
	public final String getTooltip() {
	
		return getTrayIcon() != null ? getTrayIcon().getToolTip() : null;
	}
	
	/**
	 * @return the trayIcon or null
	 */
	public final TrayIcon getTrayIcon() {
	
		return getSystemTray().getTrayIcons().length > 0 ? getSystemTray().getTrayIcons()[0] : null;
	}
	
	/**
	 * @param icon
	 *        the icon to set
	 */
	public final void setIcon(final Image icon) {
	
		//
		if(getTrayIcon() != null) {
			
			getTrayIcon().setImage(icon);
		}
	}
	
	/**
	 * @param popupMenu
	 *        the popupMenu to set
	 */
	public final void setPopupMenu(final PopupMenu popupMenu) {
	
		//
		if(getTrayIcon() != null) {
			
			getTrayIcon().setPopupMenu(popupMenu);
		}
	}
	
	/**
	 * @param tooltip
	 *        the tooltip to set
	 */
	public final void setTooltip(final String tooltip) {
	
		//
		if(getTrayIcon() != null) {
			
			getTrayIcon().setToolTip(tooltip);
		}
	}
}
